﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ECOSYSTEM_Dominio;

namespace ECOSYSTEM_DAL
{
    public class MarcaDAL
    {
        SqlConnection cnn;
        Int32 SiHay;
        public Boolean guardarMarca(Marca laMarca) { 
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlCommand cmd = new SqlCommand("RegistrarMarca",cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MarcaID", SqlDbType.Int);
                cmd.Parameters["@MarcaID"].Direction = ParameterDirection.InputOutput;
                cmd.Parameters.Add("@NomMarca", SqlDbType.NVarChar,100);
                cmd.Parameters.Add("@DescMarca", SqlDbType.Text);

                cmd.Parameters["@MarcaID"].Value = laMarca.MarcaID;
                cmd.Parameters["@NomMarca"].Value = laMarca.Nombre;
                cmd.Parameters["@DescMarca"].Value = laMarca.Descripcion;
                cmd.ExecuteNonQuery();
                laMarca.MarcaID =(Int32)(cmd.Parameters["@MarcaID"].Value);
            }
            catch (SqlException ex) { throw new Exception(ex.Message); }
            catch (Exception e) { throw new Exception(e.Message); }
            finally
            {
                if (cnn.State == ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
            return true;
        }

        public DataTable ObtenerMarca() {
            DataTable lista = null;
            try {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlDataAdapter da = new SqlDataAdapter("ObtenerMarca",cnn);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                lista = new DataTable();
                da.Fill(lista);
            }
            catch (SqlException ex) { throw new Exception(ex.Message); }
            catch (Exception e) { throw new Exception(e.Message); }
            finally
            {
                if (cnn.State == ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
            return lista;
        }     

        public Boolean ActualizarMarca(Marca laMarca)
        {
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlCommand cmd = new SqlCommand("ActualizarMarca", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MarcaID", SqlDbType.Int);
                cmd.Parameters.Add("@NomMarca", SqlDbType.NVarChar, 100);
                cmd.Parameters.Add("@DescMarca", SqlDbType.Text);

                cmd.Parameters["@MarcaID"].Value = laMarca.MarcaID;
                cmd.Parameters["@NomMarca"].Value = laMarca.Nombre;
                cmd.Parameters["@DescMarca"].Value = laMarca.Descripcion;
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex) { throw new Exception(ex.Message); }
            catch (Exception e) { throw new Exception(e.Message); }
            finally
            {
                if (cnn.State == ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
            return true;
        }

        public Int32 NuevaMarcaCod(String nombreMarca, String DescripcionMarca)
        {
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlCommand cmd = new SqlCommand("ObtenerUltimoCodigoMarca", cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@NomMarca", SqlDbType.NVarChar, 100);
                cmd.Parameters.Add("@DescMarca", SqlDbType.Text);
                cmd.Parameters["@NomMarca"].Value = nombreMarca;
                cmd.Parameters["@DescMarca"].Value = DescripcionMarca;
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    SiHay = Convert.ToInt32(dr["NewCodMarca"]);
                }
            }
            catch (SqlException e) { throw new Exception(e.Message); }
            catch (Exception ex) { throw new Exception(ex.Message); }
            finally
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
            }
            return SiHay;
        }
    }
}
